'''
Created on 11/07/2010

@author: Daniel
'''
import unittest
from Program import *
from CPUInstruction import *
from multiprocessing import Queue
from IOInstructionPrinter import *
from IOInstructionScreen import *
from IOInstructionKeyBoard import *  
from ManagerCPU import *
from OS import *
import unittest


class FifoTest(unittest.TestCase):
    
    
    
    def setUp(self):
        self.outputQueue = Queue()

        self.scheduler = FF()
        
        self.rm = ResourceManager()
        self.rm.registerResource("kb", 1, self.scheduler.readyQueue, self.outputQueue)
        self.rm.registerResource("screen", 1, self.scheduler.readyQueue, self.outputQueue)
        self.rm.registerResource("printer", 1, self.scheduler.readyQueue, self.outputQueue)
        
        self.cpuManager = CPUManager(1, self.rm,self.outputQueue,self.scheduler)
         
        self.myOS = OS(self.scheduler, self.rm,self.cpuManager,self.outputQueue)
        self.myProg = Program()
        self.myProg.setPriority(1)
        self.myProg2 = Program()
        self.myProg3 = Program()
        self.myProg2.setPriority(2)
        self.in11=CPUInstruction(1,"cpu prog2")
        self.in1=CPUInstruction(1,"cpu prog")
        self.in3=IOInstructionPrinter(3,"printer") 
        self.in5=IOInstructionScreen(2,"screen")
        self.in6=IOInstructionKeyBoard(2,"kb")
        
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in5)
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in5)
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in1)
        
        self.myProg2.addInstruction(self.in11)
        self.myProg2.addInstruction(self.in11)
        

    def tearDown(self):
        self.myOS = None
        self.myProg = None
        
        self.myProg2 = None
        self.myProg3 = None
        
        self.in11= None
        self.in1= None
        self.in3= None
        self.in5= None
        self.in6= None
        

    def testPriorityLongOutput(self):

        # arrange
        self.myOS.addProgram(self.myProg2)
        self.myOS.addProgram(self.myProg)
        
        #act
        self.myOS.startUP()
        time.sleep(15)
        #self.myOS.shutDown()

        #assert
        self.assertEqual(self.outputQueue.qsize(), 9)
        self.assertEqual(self.outputQueue.get(),("cpu prog2", 1)) 
        self.assertEqual(self.outputQueue.get(),("cpu prog2", 1)) 
        self.assertEqual(self.outputQueue.get(),("cpu prog", 1)) 
        self.assertEqual(self.outputQueue.get(),("cpu prog", 1)) 
        self.assertEqual(self.outputQueue.get(),("screen", 2))
        
        
    #===========================================================================
    # def testPriorityFirstOutput(self):
    #    self.assertEqual(self.outputQueue._get(), "cpu prog2") # cpu prog esta asignado a myprog q tiene priority 1
    #===========================================================================


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
suite = unittest.TestLoader().loadTestsFromTestCase(fifoTest)
unittest.TextTestRunner(verbosity=2).run(suite)
